<?php
namespace app\index\controller;

use think\Controller;

use think\Model;

class Index extends Controller
{
    public function issetCookie()
    {
        if (isset($_COOKIE["name"]))
            return true;
        else
            return false;
    }

    public function introduce()
    {
        return $this->fetch();
    }

    public function index()
    {   
        $active=Model('active');
        $match=Model('match');
        $res=$match->where('id',1)->find();
        $msg=$active->select();
        $this->assign('msg',$msg);
        $this->assign('text',$res['text']);
        $this->assign('name',cookie('name'));
        return $this->fetch();
    }

    public function login()
    {
        $id=input('param.id');
        if($id==""){
            $this->assign('id',0);
            return $this->fetch();
        }
        else{
            $this->assign('id',$id);
            return $this->fetch();    	
        }
    }

    public function cancel()
    {
        cookie('name',null);
        cookie('xh',null);
        cookie('lv',null);
        $active=Model('active');
        $match=Model('match');
        $nres=$match->where('id',1)->find();
        $nmsg=$active->select();
        $this->assign('msg',$nmsg);
        $this->assign('text',$nres['text']);
        return $this->fetch('index');
    }

    public function cookie()
    {
        $id=input('get.id');
        $name=input('post.name');
        $psw=input('post.psw');
        $model = Model('user');
        $res = $model->where('username',$name)->where('password',md5($psw))->find();
        if($res['forbid']==1){
            $this->error('您当前被禁止登录,请联系管理员解封','login',5);
        }
        else if($res == ""){
            $this->error('用户名或密码错误','login',5);
        }
        else{
            $message=Model('message');
            $msg=$message->where('uid',$res['id'])->find();
            cookie('xh',$name,3600);
            cookie('name',$msg['xm'],3600);
            cookie('lv',$res['lv'],3600);
            if($id==0){
                $active=Model('active');
                $match=Model('match');
                $nres=$match->where('id',1)->find();
                $nmsg=$active->select();
                $this->assign('msg',$nmsg);
                $this->assign('text',$nres['text']);
                $this->assign('name',$msg['xm']);
                return $this->fetch('index'); 
            }
            else{
                $this->redirect("Together/c_insert",array("id"=>$id));
            }            
        }
    }

    public function change()
    {
        $name=input('post.name');
        $psw=input('post.psw');
        $npsw=input('post.npsw');
        $confirm=input('post.confirm');
        $user=Model('user');
        $res=$user->where('username',$name)->where('password',md5($psw))->find();
        if($res == ""){
            $this->error('用户名或密码错误','change',3);
        }
        else{
            if($npsw==$confirm){
                $res['password']=md5($npsw);
                $res->save();
                cookie('name',null);
                cookie('xh',null);
                cookie('lv',null);
                $this->success('修改成功','Index/index',3);
            }
            else{
               $this->error('两次密码不一致','change',3); 
            }
        }
    }

    public function active()
    {
    	return $this->fetch();
    }

    public function media()
    {
    	return $this->fetch();
    }

    public function competition()
    {
    	return $this->fetch();
    }

    public function office()
    {
    	return $this->fetch();
    }

    public function message()
    {
        return $this->fetch();
    }

    public function register_()
    {
        return $this->fetch();
    }

    public function re_insert()
    {
        $usr=input('post.usr');
        $name=input('post.name');
        $wx=input('post.wx');
        $qq=input('post.qq');
        $dh=input('post.dh');
        $email=input('post.email');
        $user=Model('user');
        $message=Model('message');
        $user->username=$usr;
        $user->password=md5($usr);
        $user->lv=0;
        $user->forbid=0;
        $user->save();
        $res=$user->where('username',$usr)->find();
        $message->xm=$name;
        $message->wx=$wx;
        $message->qq=$qq;
        $message->dh=$dh;
        $message->uid=$res['id'];
        $message->email=$email;
        $message->forbid=0;
        $message->cy="";
        $message->save();
        $this->success('注册成功','Index/register_',2);
    }
}